package com.example.zqm.git72.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zqm.git72.Activity.CollectClubActivity;
import com.example.zqm.git72.Activity.CollectMasterActivity;
import com.example.zqm.git72.Activity.CollectTrainActivity;
import com.example.zqm.git72.Activity.MySignActivity;
import com.example.zqm.git72.JavaBean.Mine;
import com.example.zqm.git72.JavaBean.User;
import com.example.zqm.git72.Model.UserModel;
import com.example.zqm.git72.R;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MyFragment extends Fragment {
    private TextView user_name,edit;
    private String user_id;
    private  Button  collect_club;
    private  Button  collect_master;
    private  Button  my_order;
    private  Button collect_train;
    private  EditText edit_password;
    private  String password;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        showUserInfo();
        user_name= (TextView) view.findViewById(R.id.user_name);
        edit_password=(EditText)view.findViewById(R.id.edit_password);
        collect_club=(Button) view.findViewById(R.id.collect_club);
        collect_master=(Button) view.findViewById(R.id.collect_master);
        collect_train=(Button)view.findViewById(R.id.collect_train);
        my_order=(Button)view.findViewById(R.id.my_order);
        edit=(TextView)view.findViewById(R.id.edit) ;
        initTT();
        initGG();
        return view;
    }
   private  void  initTT(){
       collect_club.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent =new Intent();
               intent.setClass(getActivity(), CollectClubActivity.class);
               startActivity(intent);
           }
       });
       collect_master.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent =new Intent();
               intent.setClass(getActivity(), CollectMasterActivity.class);
               startActivity(intent);
           }
       });
       my_order.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent =new Intent();
               intent.setClass(getActivity(), MySignActivity.class);
               startActivity(intent);
           }
       });
       collect_train.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent=new Intent();
               intent.setClass(getActivity(), CollectTrainActivity.class);
               startActivity(intent);
           }
       });

   }
    private void showUserInfo() {
        SharedPreferences sharedPreferences=getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id=sharedPreferences.getString("user_id","2");
       // userpass=sharedPreferences.getString("userpass","2");
        UserModel userModel =new UserModel();
        Call<Mine> call =userModel.getUser(user_id);
        call.enqueue(new Callback<Mine>() {
            @Override
            public void onResponse(Call<Mine> call, Response<Mine> response) {
                Mine userInfo = response.body();
                user_name.setText(userInfo.getUser_name());
            }
            @Override
            public void onFailure(Call<Mine> call, Throwable t) {
                Toast.makeText(getActivity(),"fail",Toast.LENGTH_SHORT).show();

            }
        });
    }
     public void initGG(){
         edit.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 password=edit_password.getText().toString();
                 UserModel userModel =new UserModel();
                 Call<Integer> call= userModel.update(user_id,password);
                 call.enqueue(new Callback<Integer>() {
                     @Override
                     public void onResponse(Call<Integer> call, Response<Integer> response) {
                         Toast.makeText(getActivity(),"修改成功",Toast.LENGTH_SHORT).show();
                     }

                     @Override
                     public void onFailure(Call<Integer> call, Throwable t) {

                     }
                 });

             }
         });
     }

}
